package com.funong.matrix.core.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
@AllArgsConstructor
public enum ResultCodeEnum {
    /**
     * 返回值枚举
     */

    OK(0, "处理成功"),
    WEBSOCKET_SUCCESS(2, "websocket连接成功"),
    /**
     * 系统异常
     */
    FAILURE(1, "系统异常"),
    // http状态
    UNAUTHORIZED(401, "Unauthorized-未授权"),
    SYSTEM_INNER_ERROR(403, "系统繁忙，请稍后重试"),
    SYSTEM_NO_MESSAGE_ERROR(404, "No message available"),

    PARAM_ERROR(502, "参数非法"),
    FAILED_DEL_OWN(503, "不能删除自己"),
    FAILED_USER_ALREADY_EXIST(504, "该用户已存在"),
    INTERNAL_SERVER_ERROR(505, "服务器内部错误"),
    BIZ_ERROR(506, "业务异常"),
    RECORD_NOT_EXIST(507, "记录不存在"),
    REMOTE_SERVICE_ERROR(508, "远程服务调用失败"),
    RESULT_HTTP_CODE_ERROR(509, "HTTP响应码错误!"),
    RESULT_JSON_ERROR(510, "json解析错误!"),
    RESULT_IO_ERROR(511, "网络IO错误!"),
    REPEATED_SUBMIT(512, "禁止重复提交,请稍等片刻!"),

    WECHAT_USER_PERMISSION(48001, "该公众号没有获取用户信息权限！"),

    PARAMS_MISS(50001, "缺少接口中必填参数"),
    RECORD_REPEAT(50002, "重复数据"),

    /**
     * 用户模块错误：20001-29999
     */
    //用户登录
    USER_NOT_LOGGED_IN(100001, "用户登录失效"),
    USER_FORBIDDEN(100001, "用户被禁用"),
    USER_LOGIN_ERROR(100002, "账号不存在或密码错误"),
    USER_HAS_EXISTED(100003, "用户已存在"),
    USER_NOT_REGISTER(100004, "用户未注册"),
    USER_LOGGED_IN_FAILURE(100005, "用户登录失效,请重新登录"),
    USER_AUTH_FAILURE(100006, "认证失败"),
    USER_NOT_EXIST(100007, "用户不存在"),
    USER_USERNAME_EXIST(100008, "用户名已存在"),
    USER_PASSWORD_ERROR(100009, "用户密码错误"),
    USER_PASSWORD_INITIAL(100010, "不能使用初始密码"),
    USER_PASSWORD_SAME(100011, "密码相同"),
    USER_PHONE_EXIST(100012, "手机号已存在"),
    USER_LOGGED_FAILURE(100005, "用户验证失败"),
    // 用户管理
    USER_CREATE_FAILED(100011, "用户新增失败!"),
    USER_UPDATE_FAILED(100012, "用户修改失败!"),
    USER_DELETE_FAILED(100013, "用户删除失败!"),
    FORCED_CHANGE_PASSWORD(100014, "请修改密码"),
    PLEASE_CHANGE_PASSWORD(100015, "建议您修改密码"),
    USER_PASSWORD_EXPIRED(1000016, "密码过期,请修改密码"),
    //用户权限
    PERMISSION_NO_ACCESS(100101, "无访问权限"),
    //部门异常信息
    DEPT_NAME_NOT_EXIST(100201, "部门名称不能为空"),
    DEPT_NOT_FIND(100202, "部门不存在"),
    DEPT_UPDATE_FAILED(100203, "部门修改失败"),
    DEPT_CREATE_FAILED(100204, "部门创建失败"),
    DEPT_DELETE_FAILED(100205, "部门删除失败"),
    DEPT_NOT_EXIST_SUBDEPTS(100206, "该部门不存在子部门"),
    DEPT_SUB_LESSTRAN_PARENT(100207, "子部门等级要比父部门等级小一级"),
    DEPT_NOT_IN_SAME_COMPANY(100207, "父部门和子部门不在同一公司"),
    DEPT_LEADER_NOT_FIND(100208, "部门领导不存在"),
    DEPT_SUB_NOT_SAME_WITH_PARENT(100209, "该部门与其父部门不能为同一部门"),
    DEPT_LEADER_NOT_SELECT(100210, "部门领导未选择"),
    DEPT_SAVE_PARENT_COMP(100210, "请在子公司下新增部门"),
    //公司
    COMP_NAME_NOT_EXIST(100301, "公司名称不能为空"),
    COMP_NOT_FIND(100302, "公司不存在"),
    COMP_UPDATE_FAILED(100303, "公司修改失败"),
    COMP_CREATE_FAILED(100304, "公司创建失败"),
    COMP_DELETED_FAILED(100305, "公司删除失败"),
    //角色
    ROLE_CREATE_FAILED(100401, "角色新增失败!"),
    ROLE_UPDATE_FAILED(100402, "角色修改失败!"),
    ROLE_DELETE_FAILED(100403, "角色删除失败!"),
    POWER_CREATE_FAILED(100404, "权限新增失败!"),
    POWER_UPDATE_FAILED(100405, "权限修改失败!"),
    POWER_DELETE_FAILED(100406, "权限删除失败!"),
    USER_ROLE_CREATE_FAILED(100407, "用户添加角色失败!"),
    ROLE_POWER_CREATE_FAILED(100408, "角色添加权限失败!"),
    USER_ROLE_DELETE_FAILED(100409, "用户删除角色失败!"),
    ROLE_POWER_DELETE_FAILED(100410, "角色删除权限失败!"),
    ROLE_NAME_HAS_EXISTED(100411, "角色名已存在"),
    ROLE_NOT_EXISTED(100412, "角色不存在"),
    POWER_NAME_HAS_EXISTED(100413, "权限名已存在"),
    POWER_NOT_EXISTED(100414, "权限不存在"),
    MENU_NAME_HAS_EXISTED(100415, "菜单名已存在"),
    MENU_NOT_EXISTED(100416, "菜单不存在"),
    MENU_CREATE_FAILED(100417, "菜单新增失败!"),
    MENU_UPDATE_FAILED(100418, "菜单修改失败!"),
    MENU_DELETE_FAILED(100419, "菜单删除失败!"),
    MENU_EMPUTY(100500, "菜单为空!"),
    ROLE_EMPUTY(100501, "角色不能为空!"),
    ROLE_CODE_EXISTED(100502, "角色编号已存在"),
    PERMISSION_CODE_EXISTED(100503, "权限编号已存在"),
    PERMISSION_PARENT_CODE_NOT_EXISTED(100504, "父级权限编号不存在"),


    EMPLOY_PARAM_ERROR(100420, "账号数据错误!"),
    EMPLOY_FORBIDDEN(100421, "账号被禁用,请联系管理员"),
    EMPLOY_NOT_ONLY_ONE(100422, "手机绑定账号不唯一,请联系管理员!"),
    EMAIL_ERROR(100423, "已存在相同邮箱用户,请修改"),
    EMAIL_PHONE_ERROR(100424, "已存在相同邮箱,手机号用户,请修改"),
    PHONE_NOT_ONLY_ONE(100425, "已存在相同手机号用户,请修改"),
    NAME_CODE_ERROR(100426, "已存在相同名称和编号角色,请修改"),
    EMPLOY_NOT_EXISTED(100427, "账号不存在"),
    LKG_ERROR(100428, "已存在相同微软或LKG编号,请修改"),

    /**
     * 用户标签模块
     */
    FAILED_TAG_NAME_EXIST(100501, "标签已存在"),
    FAILED_TAG_BE_USED(100502, "标签被使用，不可删除"),
    TAG_CREATE_FAILED(100503, "标签创建失败"),
    TAG_DELETE_FAILED(100504, "标签删除失败"),
    /**
     * 分类
     */
    CATEGORY_NAME_EXIST(100601, "分类已存在"),
    CATEGORY_CREATE_FAILED(100602, "分类创建失败"),
    CATEGORY_UPDATE_FAILED(100603, "分类修改失败"),
    CATEGORY_DELETE_FAILED(100604, "分类删除失败"),

    /**
     * 账号
     */
    PHONE_NOT_SAME(100701, "手机号不一致"),
    VERIFY_CODE_NOT(100702, "验证码过期,请重新获取"),
    VERIFY_CODE_ERROR1(100702, "验证码错误"),
    PASSWORD_THE_SAME(100703, "新密码与原密码不能一样"),
    /**
     * 消息模块
     */
    MESSAGE_SMS_SEND_FAIL(150000, "短信发送失败"),
    MESSAGE_ERROR_OR_EXPIRED(150001, "验证码不正确或已过期"),
    PHONE_ERROR(150002,"手机号码格式非法或不能为空"),
    VERIFY_CODE_ERROR(150003,"验证码错误"),
    VERIFY_CODE_REPEATED(150004,"请勿重复发送验证码,请60秒以后再试"),

    /**
     * 文件
     */
    FILE_SERVER_ERROR(160004, "文件服务器连接失败"),
    FILE_UPLOAD_FAIL(160000, "文件上传失败"),
    DOWNLOAD_FAILED(160001, "下载文件不存在!"),
    FILE_EMPTY(160002, "上传文件为空"),
    DELETE_FAIL(160003, "删除文件失败"),
    DEAL_FAIL(160005, "文件处理失败"),

    SELECT_TRADE_DAY(170001, "请选择交易日日期～"),



    ;



    private int code;
    private String msg;


    ResultCodeEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer code() {
        return this.code;
    }

    public String msg() {
        return this.msg;
    }

    public static String getMsg(String name) {
        for (ResultCodeEnum item : ResultCodeEnum.values()) {
            if (item.name().equals(name)) {
                return item.msg;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ResultCodeEnum item : ResultCodeEnum.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.name();
    }

    public static ResultCodeEnum valueOf(Integer code) {
        return Arrays.stream(values())
                .filter(resultCode -> Objects.equals(code, resultCode.code))
                .findFirst()
                .orElse(OK);
    }

    public static ResultCodeEnum getByMsg(String msg) {
        return Arrays.stream(values())
                .filter(resultCode -> Objects.equals(msg, resultCode.msg))
                .findFirst()
                .orElse(USER_LOGGED_IN_FAILURE);
    }
}
